package gaml.text.printer;

import gaml.core.GamlConfig;
import gaml.error.GamlException;
import gaml.text.YamlLine;
import gaml.text.YamlNodePrinter;
import gaml.text.YamlText;
import gaml.yaml.ScalarNode;


/**
 * makes a text representation of scalar yaml node
 * @author shnireck
 *
 */
public class ScalarNodePrinter implements YamlNodePrinter <ScalarNode> {
	final static String emptyLineError = "Scalar node value is empty";
	GamlConfig config = null;
	
	public ScalarNodePrinter (GamlConfig config) { this.config = config; }
	
	@Override
	public Class <ScalarNode> getPrintNodeClass () { return ScalarNode.class; }

	@Override
	public ScalarNodePrinter print (ScalarNode node, YamlText text) throws GamlException {
		String [] lines = (null == node.getValue () ? "" : node.getValue ()).split (config.getPrinterSettings ().getLineBreak ());
		
//		if (0 == lines.length) throw new GamlException (emptyLineError);
		if (0 == lines.length) return this;
		
		text.addLine (new YamlLine ()
								.setIdent (node.getLevel ())
								.setTag (node.getTag ())
								.setAnchor (null == node.getAnchor () ? "" : node.getAnchor ().getCode ())
								.setValue (String.format ("> %s",lines [0])));
		
		for (int i=1; i<lines.length; i++)
			text.addLine (new YamlLine ()
									.setValue (lines [i])
									.setIdent (node.getLevel ()));
		
		return this;
	}
}
